//
//  SHEHistoricalRecordVC.m
//  SHEPuzzleFigure
//
//  Created by SW mini on 2019/11/25.
//  Copyright © 2019 Figure. All rights reserved.
//

#import "SHEHistoricalRecordVC.h"
#import "SHERecordCell.h"


@interface SHEHistoricalRecordVC ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *mainTableView;
@property (nonatomic, strong) NSMutableArray *listArr;

@end

@implementation SHEHistoricalRecordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [super createBackBtn];
    if (_type == 1) {
        [super setTitleView:@"排行榜"];
    }else{
        [super setTitleView:@"历史记录"];
    }
    
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    self.mainTableView.separatorStyle = NO;
    [self.mainTableView registerNib:[UINib nibWithNibName:@"SHERecordCell" bundle:nil] forCellReuseIdentifier:@"SHERecordCell"];
    [self.view addSubview:self.mainTableView];
    
    self.listArr = [NSMutableArray arrayWithCapacity:0];
    
    if (_type == 1) {
        //获取排行榜
        [self getListData];
    }else{
        //获取本地历史
        [self getHistory];
    }
    //MJ
    self.mainTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self refreshMore];
    }];
    [self.mainTableView.mj_footer endRefreshingWithNoMoreData];
}

- (void)getListData {
    [SHENetworkObj postwithUrl:API_RANKLIST success:^(NSDictionary * _Nullable dict) {
        NSLog(@"%@",dict);
        if ([dict[@"code"] intValue]==0) {
            NSArray *arr = dict[@"data"];
            for (int i=0; i<arr.count; i++) {
                [self.listArr addObject:arr[i]];
            }
            [self.mainTableView reloadData];
        }
    } failure:^(NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
    }];
}

// 获取历史
- (void)getHistory {
    self.listArr = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"HISTORYLIST"]];
    [self.mainTableView reloadData];
}

- (void)refreshMore {
    
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    static NSString *cellID = @"SHERecordCell";
    SHERecordCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[SHERecordCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    NSDictionary *dic = self.listArr[indexPath.row];
    
    NSString *url = [NSString stringWithFormat:@"%@%@",API_HOST,dic[@"image_url"]];
    [cell.iconImgview yy_setImageWithURL:[NSURL URLWithString:url] placeholder:nil];
    cell.titleLab.text = dic[@"title"];
    cell.oneLab.text = [NSString stringWithFormat:@"难度:%@",dic[@"type"]];
    cell.twoLab.text = [NSString stringWithFormat:@"时间:%@S",dic[@"time"]];
    cell.threeLab.text = [NSString stringWithFormat:@"步数:%@",dic[@"step_num"]];
    if (_type == 1) {
        cell.noLab.text = [NSString stringWithFormat:@"NO.%ld",(long)indexPath.row+1];
    }else{
        cell.noLab.hidden = YES;
    }
    
    return cell;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.listArr.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 104;
}

@end
